//
//  MoreCollection.m
//  FKGW
//
//  Created by song ce on 2017/2/25.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "MoreCollection.h"
#import "BaseNavigationViewController.h"
#import "UIView+Extend.h"
#import "FurnitureViewController.h"



#define KColumCount 3
@implementation MoreCollection
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self _initSubviews];
        self.backgroundColor = DEF_COLOR242;
    }
    return self;
}
-(void)setDataSoure:(NSMutableArray *)dataSoure{
    _dataSoure = dataSoure;
    [_myCollectionView reloadData];
}

- (void)_initSubviews{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //每一行之间的最小距离
    layout.minimumLineSpacing = 1;
    //同一行item之间的最小间隙
    layout.minimumInteritemSpacing =1;
    //设置滑动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    UICollectionView *collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:layout];
    _myCollectionView = collectionV;
    collectionV.showsVerticalScrollIndicator = NO;
    //设置分页
    //    collectionV.pagingEnabled = YES;
    [self addSubview:collectionV];
    collectionV.backgroundColor = [UIColor colorWithRed:242./255 green:242./255 blue:242./255 alpha:1.];

    //设置代理
    collectionV.delegate = self;
    collectionV.dataSource = self;
    //注册
    [collectionV registerNib:[UINib nibWithNibName:@"MoreCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"MoreCollectionCell"];
    
    //注册头视图
    //    [collectionV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    [collectionV registerNib:[UINib nibWithNibName:@"CollectionHeadV" bundle:nil]  forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeadV"];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;{
    return self.dataSoure.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;{
    MoreGroup *group = self.dataSoure[section];
    return group.contents.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;{
    MoreCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MoreCollectionCell" forIndexPath:indexPath];
    MoreGroup *group = self.dataSoure[indexPath.section];
    NSArray *contents = group.contents;
    cell.model = contents[indexPath.row];
    return cell;
}

//创建头视图或者尾视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    CollectionHeadV *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeadV" forIndexPath:indexPath];
    MoreGroup *group = self.dataSoure[indexPath.section];
    view.group = group;
    return view;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
{
    return CGSizeMake(Main_Screen_Width, 30);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    CGFloat with = (Main_Screen_Width-2*10-KColumCount)/KColumCount;
//    CGFloat height = with*.4;
//    return CGSizeMake(with, height);
//    MoreGroup *group = self.dataSoure[indexPath.section];
//    NSArray *contents = group.contents;
//   MoreModel *model = contents[indexPath.row];
//    CGRect frame = [model.name boundingRectWithSize:CGSizeMake(0, 21) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil];
//    CGFloat with = CGRectGetWidth(frame)+10;
//    return CGSizeMake(with, 21);
    CGFloat with = (Main_Screen_Width-2*12-2)/3;
    return CGSizeMake(with, 45);

}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    UIEdgeInsets edge = UIEdgeInsetsMake(7, 12, 0 ,12);
    return edge;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    MoreGroup *group = self.dataSoure[indexPath.section];
    NSArray *contents = group.contents;
    MoreModel *model = contents[indexPath.row];
    FurnitureViewController *fvc = [[FurnitureViewController alloc]init];
    fvc.hidesBottomBarWhenPushed = YES;
    fvc.vcName = model.name;
    fvc.tradeId = model.id;
    [[self secondNavigationController] pushViewController:fvc animated:YES];
}

- (BaseNavigationViewController*)secondNavigationController{
    UIResponder *next = self.nextResponder;
    while (next!=nil) {
        if([next isKindOfClass:[BaseNavigationViewController class]]){
            return (BaseNavigationViewController*)next;
        }
        next = next.nextResponder;
    }
    return nil;
}

@end
